import axios, { AxiosResponse } from "axios";
import { StoreModel } from "../models/Store";
import { TakeoutModel } from "../models/Takeout";

class StoreApis {
    queryStore(id: string): Promise<StoreModel> {
        const promise: Promise<StoreModel> = new Promise((resolve, reject) => {
            let store: StoreModel = {
                user: id
            };
            axios.get('/api/store/store', { params: store })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let store = reslut.data.data;
                        resolve(store);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryTakeoutStore(id: string): Promise<StoreModel> {
        const promise: Promise<StoreModel> = new Promise((resolve, reject) => {
            let store: TakeoutModel = {
                store: id
            };
            axios.get('/api/store/query-store', { params: store })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        let store = reslut.data.data;
                        resolve(store);
                    } else {
                        reject(reslut.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    applyStore(store: StoreModel): Promise<boolean> {
        const promise: Promise<boolean> = new Promise((resolve, reject) => {
            axios.post('/api/store/store', store)
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code === 200) {
                        resolve(true);
                    } else {
                        resolve(false);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

export const storeApis: StoreApis = new StoreApis();